package com.tiantianyuegou.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.tiantianyuegou.activity.BaseActivity;
import com.tiantianyuegou.R;
import com.tiantianyuegou.adapter.AreaAdapter;
import com.tiantianyuegou.constant.Constant;
import com.tiantianyuegou.http.ApiDefine;
import com.tiantianyuegou.model.AreaModel;
import com.tiantianyuegou.util.Tools;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 选择省的界面
 * Created by ivany on 2016/1/14.
 */
public class SelectProviceActivity extends BaseActivity implements AdapterView.OnItemClickListener {

//http://hjtech.wicp.net:8080/tiantianyuegou-api/region/provice/list
    private ListView mListView;
    private List<AreaModel> mData;
    private AreaAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_area);
       Tools.changeSystemBar(this);
        setTitle("选择省");
        initViews();
    }

    @Override
    public void findViewByIds() {
        mListView = (ListView) findViewById(R.id.select_area_list);
    }

    @Override
    public void initListener() {
        mListView.setOnItemClickListener(this);
    }

    @Override
    public void initData() {
        mData=new ArrayList<>();
        getDate("1", "");
        mAdapter = new AreaAdapter(this);
        mListView.setAdapter(mAdapter);
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(SelectProviceActivity.this, SelectCityActivity.class);
        intent.putExtra("id", mData.get(position).getId()+"");
        Constant.proviceName=mData.get(position).getName();
        Constant.proviceID=mData.get(position).getId();
        startActivityForResult(intent, Constant.SELECT_CITY_REQUEST);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Constant.SELECT_CITY_REQUEST) {
            if (resultCode == Constant.SELECT_CITY_RESULT) {
                Intent intent = new Intent();
                setResult(Constant.SELECT_PROVICE_RESULT, intent);
                finish();
            }
        }
    }

    public void getDate(String type, String id) {
        RequestParams params = new RequestParams();
        params.addBodyParameter("type", type);
        params.addBodyParameter("id", id);
        loadData(HttpRequest.HttpMethod.POST, ApiDefine.SELECT_AREA, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {

                String returnstr = responseInfo.result;
                Log.e("TAG", returnstr);
                if (TextUtils.isEmpty(returnstr)) {
                    Toast.makeText(SelectProviceActivity.this, "获取失败", Toast.LENGTH_LONG).show();
                } else {
                    try {
                        JSONObject rootObject = new JSONObject(returnstr);
                        String code = rootObject.getString("code");
                        if ("100".equals(code)) {
                            mData.clear();
                            JSONArray jsonArray = rootObject.getJSONArray("result");
                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject temp = jsonArray.getJSONObject(i);
                                String name = temp.getString("name");
                                int id = temp.getInt("id");
                                AreaModel model = new AreaModel(id, name);
                                model.setLevel("1");
                                mData.add(model);
                            }
//                            TODO 需换成真是操作字段名称
                            mAdapter.setData(mData);
                            mAdapter.notifyDataSetChanged();
                        } else {
                            String msg = rootObject.getString("message");
                            Toast.makeText(SelectProviceActivity.this, msg, Toast.LENGTH_LONG).show();
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
                Toast.makeText(SelectProviceActivity.this, "当前网络不可用，请检查网络连接", Toast.LENGTH_LONG).show();
            }
        });

    }

}
